/*
 *  Copyright (C) 2010 Interactive Media Management
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.i2m.converge.jsf.beans;

import dk.i2m.converge.core.workflow.Outlet;
import dk.i2m.converge.core.security.SystemPrivilege;
import dk.i2m.converge.core.security.UserAccount;
import dk.i2m.converge.ejb.facades.CalendarFacadeLocal;
import dk.i2m.converge.ejb.facades.NewsItemFacadeLocal;
import dk.i2m.converge.ejb.facades.OutletFacadeLocal;
import dk.i2m.converge.ejb.facades.UserFacadeLocal;
import dk.i2m.converge.ejb.facades.WorkflowFacadeLocal;
import dk.i2m.converge.ejb.services.NewswireServiceLocal;
import dk.i2m.converge.jsf.components.tags.DialogAssignment;
import dk.i2m.converge.jsf.components.tags.DialogEventSelection;
import dk.i2m.jsf.JsfUtils;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.event.ActionEvent;

/**
 * JSF backing bean for {@code /NewswireItem.jspx}
 *
 * @author Allan Lykke Christensen
 */
public class NewswireItem {

    @EJB private NewswireServiceLocal newswireService;

    @EJB private OutletFacadeLocal outletFacade;

    @EJB private UserFacadeLocal userFacade;

    @EJB private CalendarFacadeLocal calendarFacade;

    @EJB private NewsItemFacadeLocal newsItemFacade;

    @EJB private WorkflowFacadeLocal workflowFacade;

    private dk.i2m.converge.core.newswire.NewswireItem item;

    private DialogAssignment dialogAssignment;

    private DialogEventSelection dialogEventSelection;

    public NewswireItem() {
    }

    public dk.i2m.converge.core.newswire.NewswireItem getItem() {
        return item;
    }

    public void setItem(dk.i2m.converge.core.newswire.NewswireItem item) {
        this.item = item;
    }

    public void onAssign(ActionEvent event) {
        dialogAssignment = new DialogAssignment(outletFacade, workflowFacade, userFacade, newsItemFacade, calendarFacade, getOutlets());
        dialogAssignment.showStoryTab();
        dialogAssignment.getAssignment().setAssigned(true);
        dialogAssignment.getAssignment().setAssignedBy(getUserAccount());
        if (!getOutlets().isEmpty()) {
            dialogAssignment.getAssignment().setOutlet(getOutlets().iterator().next());
        }
        dialogAssignment.getAssignment().setTitle(item.getTitle());
        dialogAssignment.getAssignment().setBrief(item.getSummary());
        dialogAssignment.getAssignment().setStory(item.getContent());

        dialogAssignment.onChangeOutlet(null);

        dialogAssignment.getAssignment().setDeadline(java.util.Calendar.getInstance());
        dialogAssignment.getAssignment().getDeadline().setTimeZone(getUserAccount().getTimeZone());
        dialogAssignment.getAssignment().getDeadline().set(java.util.Calendar.HOUR_OF_DAY, 15);
        dialogAssignment.getAssignment().getDeadline().set(java.util.Calendar.MINUTE, 0);
        dialogAssignment.getAssignment().getDeadline().set(java.util.Calendar.SECOND, 0);

        dialogEventSelection = new DialogEventSelection(calendarFacade);
        dialogEventSelection.setAssignment(dialogAssignment.getAssignment());
    }

    /**
     * Gets a {@link List} of the {@link Outlet}s where the current user has
     * outlet planning privileges.
     *
     * @return {@link List} of the {@link Outlet}s where the current user has
     *         outlet planning privileges
     */
    public List<Outlet> getOutlets() {
        return getUserAccount().getPrivilegedOutlets(SystemPrivilege.OUTLET_PLANNING);
    }

    private UserAccount getUserAccount() {
        final String valueExpression = "#{userSession.user}";
        return (UserAccount) JsfUtils.getValueOfValueExpression(valueExpression);
    }

    public DialogAssignment getDialogAssignment() {
        return dialogAssignment;
    }

    public void setDialogAssignment(DialogAssignment dialogAssignment) {
        this.dialogAssignment = dialogAssignment;
    }

    public DialogEventSelection getDialogEventSelection() {
        return dialogEventSelection;
    }

    public void setDialogEventSelection(DialogEventSelection dialogEventSelection) {
        this.dialogEventSelection = dialogEventSelection;
    }
}
